<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Announcement;

class AnnouncementController extends Controller {

    protected $message = [
        'title.required' => '公告标题必须',
        'content.required' => '公告内容必须',
        'site_id.required' => '请选择站点',
        'site_id.integer' => '站点参数非法',
        'site_id.exists' => '站点不存在',
        'company_id.required' => '请选择项目',
        'site_id.integer' => '项目参数非法',
        'company_id.exists' => '项目不存在',
    ];

    /**
     * 公告管理
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request) {
        $perPage = $request->input('pageSize', 15);
        $query = Announcement::query();

        $query->with([
            'company' => function($query) {
                $query->select(['Co_ID', 'Co_Name']);
            },
            'site' => function($query) {
                $query->select(['id', 'name']);
            },
        ]);

        if ($request->has('company')) {
            $companyId = $request->input('company', '');
            $query->where('company_id', '=', trim($companyId));
        }

        $siteId = $request->input('site', '');
        if (!empty($siteId)) {
            $query->where('site_id', '=', trim($siteId));
        }

        $list = $query->paginate($perPage);

        return ['data' => $list];
    }



    protected function validateData($request) {
        $v = $this->validate2($request, [
            'title' => 'required',
            'content' => 'required',
            'site_id' => 'required|integer|exists:sites,id',
                ], $this->message);
        return $v;
    }

    /**
     * 添加公告
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request) {
        $data = $request->input();
        $v = $this->validateData($request);
        $currentUser = $request->user();
        $isSuper = $currentUser->isSuper();
        $v->sometimes('company_id', 'required|integer|exists:t_companycnfo,Co_ID', function()use($isSuper) {
            return $isSuper;
        });

        $v->validate();

        if (!$isSuper) {
            $data['company_id'] = $currentUser->Co_ID;
        }
        $data['poster_id'] = $request->user()->userid;
        $data['poster_name'] = $request->user()->username;
//       $data['company_id'] = $request->user()->Co_ID;
//       $data['site_id'] = $request->user()->scope;
        $ret = Announcement::create($data);

        if (!$ret) {
            $this->error('添加失败');
        }
    }



    /**
     * 更新公告
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id) {
        $announce = Announcement::findOrFail($id);
        $data = $request->input();
        $v = $this->validateData($request);
        $currentUser = $request->user();
        $isSuper = $currentUser->isSuper();
        $v->sometimes('company_id', 'required|integer|exists:t_companycnfo,Co_ID', function()use($isSuper) {
            return $isSuper;
        });

        $v->validate();

        if (!$isSuper) {
            $data['company_id'] = $currentUser->Co_ID;
        }
        $data['poster_id'] = $request->user()->userid;
        $data['poster_name'] = $request->user()->username;
//       $data['company_id'] = $request->user()->Co_ID;
//       $data['site_id'] = $request->user()->scope;
        $ret = $announce->update($data);

        if ($ret === false) {
            $this->error('保存失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id) {
        $announce = Announcement::findOrFail($id);
        if (!$announce->delete()) {
            $this->error('删除失败');
        }
    }

}
